﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using APISDKDemo.Common;
using APISDKDemo.Models;
using APISDKDemo.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using static APISDKDemo.Common.SDKClient;

namespace APISDKDemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        //HttpClientFactory Demo
        private readonly ISampleClient _sampleClient;

        public HomeController(ILogger<HomeController> logger, ISampleClient sampleClient)
        {
            _logger = logger;
            _sampleClient = sampleClient;
        }

        #region HttpClient Demo
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Index(string appKey, string appSecret, string serverRoot)
        {

            SDKClient sdkclient = new SDKClient(appKey, appSecret, serverRoot);
            Dictionary<string, string> param = new Dictionary<string, string>();
            param.Add("id", "1");
            param.Add("name", "张三");
            param.Add("country", "中华人民共和国");

            //GET
            //SDKResult result = await sdkclient.GetAsync("/weatherforecast", param);

            //POST
            SDKResult result = await sdkclient.PostAsync("/weatherforecast", param);

            return View(nameof(Index), result);
        }
        #endregion




        #region HttpClientFactory Demo
        public IActionResult Index2()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Index2(string method = "GET")
        {
            SDKResult result = null;

            Dictionary<string, string> param = new Dictionary<string, string>();
            param.Add("id", "1");
            param.Add("name", "张三");
            param.Add("country", "中华人民共和国");

            if (method.Equals("GET"))
            {
                result = await _sampleClient.GetAsync("/weatherforecast", param);
                return View(nameof(Index2), result);
            }


            //POST
            result = await _sampleClient.PostAsync("/weatherforecast", param);
            return View(nameof(Index2), result);
        }

        #endregion





    }
}
